package com.github.thinwonton.springboot.showcase.poi.easypoi;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.github.thinwonton.springboot.showcase.poi.easypoi.entity.User;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Hyperlink;

import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * EasyPOIMain
 *
 * @author hugo
 * @date 2018/9/18
 */
public class EasyPOIMain {
    private static final String excelFilePath = "file/demo.xlsx";

    public static void main(String[] args) throws IOException {
        File file = classpathFile(excelFilePath);

//        File file = new File("D:/demo.xlsx");

        ImportParams params = new ImportParams();
        params.setStartSheetIndex(3); //从模板中第三个sheet读取
        //验证数据
        params.setNeedVerfiy(true);
        //验证数据数据，在数据库中验证
//        params.setVerifyHandler(new IExcelVerifyHandler() {
//            @Override
//            public ExcelVerifyHandlerResult verifyHandler(Object obj) {
//                return new ExcelVerifyHandlerResult(false,"数据库已经存在该数据");
////                return new ExcelVerifyHandlerResult(true);
//            }
//        });

        params.setDataHandler(new IExcelDataHandler() {
            @Override
            public Object exportHandler(Object obj, String name, Object value) {
                return null;
            }

            @Override
            public String[] getNeedHandlerFields() {
                return new String[0];
            }

            @Override
            public Object importHandler(Object obj, String name, Object value) {
                return null;
            }

            @Override
            public void setNeedHandlerFields(String[] fields) {

            }

            @Override
            public void setMapValue(Map map, String originKey, Object value) {

            }

            @Override
            public Hyperlink getHyperlink(CreationHelper creationHelper, Object obj, String name, Object value) {
                return null;
            }
        });

        ExcelImportResult<User> result = ExcelImportUtil.importExcelMore(file, User.class, params);

        List<User> successList = result.getList();
        List<User> failList = result.getFailList();

        System.out.println("是否存在验证未通过的数据:" + result.isVerfiyFail());
        System.out.println("验证通过的数量:" + successList.size());
        System.out.println("验证未通过的数量:" + failList.size());

        for (User user : successList) {
            System.out.println("成功列表信息:" + user.getName());
        }
        for (User user : failList) {
            System.out.println("失败列表信息:" + user.getName());
        }

        File failedFile = new File("d:/failed.xlsx");
        if (result.isVerfiyFail()) {
            OutputStream os = new FileOutputStream(failedFile);
            result.getFailWorkbook().write(os);
            os.close();
        }

        System.out.println("aaaa");

    }

    private static File classpathFile(String path) {
        URL systemResource = ClassLoader.getSystemResource(path);
        return new File(systemResource.getFile());
    }
}
